package com.itheima.service.Impl;

import com.itheima.mapper.PGMapper;
import com.itheima.mapper.PPGMapper;
import com.itheima.pojo.PermissionPermissionGroup;
import com.itheima.pojo.pe.PeDto;
import com.itheima.pojo.pe.pe_permission_group;
import com.itheima.service.PPGService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PPGServiceImpl implements PPGService {
    @Autowired
    private PPGMapper ppgMapper;
    @Autowired
    private PGMapper pgMapper;

    @Override
    public void addGroup(PeDto peDto) {
        pe_permission_group pg = new pe_permission_group();
        //封装数据,添加到pe_permission_group
        pg.setTitle(peDto.getTitle());
        pg.setCreateTime(LocalDateTime.now());
        pg.setUpdateTime(LocalDateTime.now());
        pgMapper.insert(pg);
        //添加到a_permission_permission_group
        List<Integer> permissions = peDto.getPermissions();
        for (Integer id : permissions) {
            PermissionPermissionGroup ppg = new PermissionPermissionGroup();
            ppg.setPgid(pg.getId());
            ppg.setPid((long)id);
            ppgMapper.insert(ppg);
        }
    }
}
